<%--
  Created by IntelliJ IDEA.
  User: 陈杰
  Date: 2020/8/5
  Time: 19:11
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    /*basePath===http://localhost:8080/TestServlet04/*/
%>
<html>
<head>
    <title>学生个人信息表</title>
    <script type="text/javascript" src="<%=basePath%>static/js/jquery-1.11.3.min.js"></script>

    <link rel="stylesheet" href="<%=basePath%>static/css/bootstrap-theme.css">
<%--    <link rel="stylesheet" href="<%=basePath%>static/css/pintuer.css">--%>


    <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css">

    <script src="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <script type="text/javascript" src="<%=basePath%>static/js/bootstrap.min.js"></script>
    <script type="text/javascript" src="<%=basePath%>static/js/jqPaginator.js"></script>
</head>
<body >
<div id="window" style="position: absolute;left: 30%;top:20%;width: 400px;height: 370px;background-color: #a6e1ec;display: none;">
    <center>
        <input type="hidden" name="id" id="id">
        <span >学生id：  </span><input type="text" name="sid" id="sid" readonly = "true"><br>
        <span>学生编号：</span><input type="text" name="sno" id="sno" readonly = "true"><br>
        <span >学生姓名：</span><input type="text" name="sname" id="sname" readonly = "true"><br>
        <span >学生密码：</span><input type="text" name="spwd" id="spwd" ><br>
        <span>入学日期：</span><input type="text" name="entrancetime" id="entrancetime" readonly = "true"><br>
        <span >性别：    </span>    <input type="text" name="sex" id="sex"><br>
        <span>地址：    </span>    <input type="text" name="address" id="address"><br>
                            <input type="button" value="修改" onclick='updateStudentById()' >
                            <input type="reset" value="重置">
                             <input type="reset" value="关闭" onclick="closeWindow()">
    </center>
</div>
<table class="table table-hover" border="1" width="50%">
    <tr>
        <td>学生id</td>
        <td>学生编号</td>
        <td>学生姓名</td>
        <td>学生密码</td>
        <td>入学日期</td>
        <td>性别</td>
        <td>地址</td>
        <td colspan="2">操作</td>
        
    </tr>
    <tbody id="tb">
    </tbody>
</table>
</body>

<script>
    //文档加载后要执行的函数
    $(document).ready(function(){
        loadData();
    });

    function loadData() {
        var sno = '<%=session.getAttribute("userId")%>';
        //jquery的ajax请求方法
        $.ajax({
            url: "<%=basePath%>student/queryStudent",
            async: false,//同步请求（默认）
            type: "POST",
            dataType: "json",
            data: {"sno": sno},
            success: function (data) {
                console.log(data);
                $("#tb").html("");
                if (data!==null) {
                    //使用jquery形式遍历
                    // $.each(data.list, function (n, val) {
                        var str = "";
                        str += "<tr>" +
                            "<td>" + data.sid+ "</td>" +
                            "<td>" + data.sno + "</td>" +
                            "<td>" + data.sname + "</td>" +
                            "<td>" + data.spwd + "</td>" +
                            "<td>" + data.entrancetime + "</td>" +
                            "<td>" + data.sex + "</td>" +
                            "<td>" + data.address + "</td>" +
                            "<td>  <button onclick='updateWindow(this)'  class=\"button border-main\" role=\"button\" >修改</button>" +
                            "</td>" +
                            "</tr>";
                        $("#tb").append(str);
                    // });
                } else {
                    $("#tb").append("<tr><td colspan=8 align=\"center\">暂时没有数据哦，快去添加一条吧</td></tr>");
                }
            },
            error: function () {
                alert("服务器出错！");
            }
        });





    }
    //打开修改的模态框
    function updateWindow(obj) {
        //显示修改的模态框
        $("#window").show();
        //获取当前选中行的所有数据
        var sid = $(obj).parent().parent().find("td").eq(0).text()
        var sno = $(obj).parent().parent().find("td").eq(1).text()
        var sname = $(obj).parent().parent().find("td").eq(2).text()
        var spwd = $(obj).parent().parent().find("td").eq(3).text()
        var entrancetime = $(obj).parent().parent().find("td").eq(4).text()
        var sex = $(obj).parent().parent().find("td").eq(5).text()
        var address = $(obj).parent().parent().find("td").eq(6).text()
        var scCheckId = $(obj).parent().parent().find("td").eq(7).text()
        var sdmmId = $(obj).parent().parent().find("td").eq(8).text()
        var scClassId = $(obj).parent().parent().find("td").eq(9).text()
        var isordelete = $(obj).parent().parent().find("td").eq(10).text()
        var studentRole = $(obj).parent().parent().find("td").eq(11).text()

        console.log("locked=" + id)
        //把选中行的数据复制给修改框里的表单元素
        $("#sid").val(sid);
        $("#sno").val(sno);
        $("#sname").val(sname);
        $("#spwd").val(spwd);
        $("#entrancetime").val(entrancetime);
        $("#sex").val(sex);
        $("#address").val(address);
        $("#scCheckId").val(scCheckId);
        $("#sdmmId").val(sdmmId);
        $("#scClassId").val(scClassId);
        $("#isordelete").val(isordelete);
        $("#studentRole").val(studentRole);

    }

    //修改操作
    function updateStudentById() {
        //获取文本框的值
        var sid = $("#sid ").val();
        var sno = $("#sno ").val();
        var sname = $("#sname ").val();
        var spwd = $("#spwd").val();
        var entrancetime = $("#entrancetime").val();
        var sex = $("#sex").val();
        var address = $("#address").val();
        var scCheckId = $("#scCheckId").val();
        var sdmmId = $("#sdmmId").val();
        var scClassId = $("#scClassId").val();
        var isordelete = $("#isordelete").val();
        var studentRole = $("#studentRole").val();
        $.ajax({
            url: "<%=basePath%>student/updateStudent",
            type: "Post",
            data: {
                // "method": "doPost",
                "sid": sid,
                "sno": sno,
                "sname": sname,
                "spwd": spwd,
                "entrancetime": entrancetime,
                "sex": sex,
                "address": address,
                "scCheckId": scCheckId,
                "sdmmId": sdmmId,
                "scClassId": scClassId,
                "isordelete": isordelete,
                "studentRole": studentRole,
            },
            success: function (data) {
                console.log("!!!!!!!!!!"+data)
                alert("修改成功")
                //关闭模态框
                $("#window").hide();
                //刷新
                 loadData();
                // window.location.reload();
            }
        })

    }
    function closeWindow() {
        $("#window").hide();
    }
</script>
</html>
